/**
 * 
 */
package com.iss.card.mapv2;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;

/**
 * please refer to http://www.cnblogs.com/xmlspy/archive/2013/01/03/2843470.html
 * @author dtdh
 * 
 */
public class MapUtility {
	private final static String APP_GOOGLE_MAP = "com.google.android.apps.maps";
	private final static String PATH_FOR_GOOGLE_PLAY_STORE = "market://details?id=";
	private Context mContext;
	private static MapUtility sInstance;
	
	public synchronized static MapUtility instance() {
		if (sInstance == null) {
			sInstance = new MapUtility();
		}
		return sInstance;
	}

	/**
	 * 
	 */
	private MapUtility() {

	}

	public void setContext(Context c) {
		mContext = c;
	}

	/**
	 * check is there google map installed.
	 * 
	 * @return true means there is google map otherwise no google map.
	 */
	public boolean isGoogleMapsInstalled() {
		try {
			mContext.getPackageManager().getApplicationInfo(APP_GOOGLE_MAP, 0);
			return true;
		} catch (PackageManager.NameNotFoundException e) {
			return false;
		}
	}

	/**
	 * jump to play store to download google maps.
	 */
	public void browseToPlayStore(){
		Intent intent = new Intent(Intent.ACTION_VIEW,
				Uri.parse(PATH_FOR_GOOGLE_PLAY_STORE + APP_GOOGLE_MAP));
		mContext.startActivity(intent);
	}
	
//	public OnClickListener getGoogleMapsListener() {
//		return new OnClickListener() {
//
//			public void onClick(DialogInterface dialog, int which) {
//				Intent intent = new Intent(Intent.ACTION_VIEW,
//						Uri.parse(PATH_FOR_GOOGLE_PLAY_STORE + APP_GOOGLE_MAP));
//				mContext.startActivity(intent);
//			}
//		};
//	}
	
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
//
//        ListAdapter adapter = new CustomArrayAdapter(this, demos);
//
//        setListAdapter(adapter);
//
//        //=======  For Google Maps Check============
//        if (!this.isGoogleMapsInstalled()) {
//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setMessage("Install Google Map ?");
//            builder.setCancelable(false);
//            builder.setPositiveButton("Install", getGoogleMapsListener());
//            AlertDialog dialog = builder.create();
//            dialog.show();
//        }
//    }
}
